package com.itfreer.bpm.workflow.option;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.itfreer.bpm.flow.api.IBpmProjectEntity;
import com.itfreer.bpm.history.entity.BpmHistoryEntity;
import com.itfreer.bpm.option.para.OptionInfo;
import com.itfreer.bpm.option.para.TaskInfo;
import com.itfreer.bpm.power.para.UserInfo;
import com.itfreer.bpm.workflow.drive.IWorkFlowDrive;
import com.itfreer.bpm.workflow.utils.BpmHistoryEditUtil;
import com.itfreer.bpm.workflow.utils.BpmHistoryQueryUtil;
import com.itfreer.bpm.workflow.utils.BpmNextTaskUtils;

/**
 * 普通项目选择节点性质
 * @author gj
 *
 */
@Component
public class SignCommonTask {
	
	/**
	 * 流程驱动服务
	 */
	@Autowired
	protected IWorkFlowDrive workFlowDrive;
	
	/**
	 * 构建历史帮转工具
	 */
	@Autowired
	BpmHistoryEditUtil historyEditUtl;
	
	
	/**
	 * 审批过程查询接口
	 */
	@Autowired
	BpmHistoryQueryUtil historyQueryUtil;
	
	
	/**
	 * 下一个环节工具
	 */
	@Autowired
	BpmNextTaskUtils nextTaskUtils;
	/**
	 * 环节提交工作
	 * @param oInfo
	 * @param entity
	 * @param user
	 * @param o_sassignees
	 * @param overWrokH
	 * @param fromTaskName
	 * @param fromTaskID
	 * @param ComValue
	 */
	public void signTask(OptionInfo oInfo, IBpmProjectEntity entity, UserInfo user, String o_sassignees,
			BpmHistoryEntity overWrokH, String fromTaskName, String fromTaskID, HashMap<String, Object> ComValue,String historyStatus) {
		int index=0;
		List<BpmHistoryEntity> addlst=new ArrayList<BpmHistoryEntity>();
		List<BpmHistoryEntity> updatelst=new ArrayList<BpmHistoryEntity>();
		//HashMap<String,Object> comTask=new HashMap<String,Object>();
		for(TaskInfo itask:oInfo.getNextTask()) {
			if(itask.isTaskSelect()) {
				
				//comTask.put(itask.getTaskdefinedid(), "提交");
				HashMap<String, Object> where=new HashMap<String, Object>();
				where.put("sexeid:=", oInfo.getSexeid());
				where.put("taskdefid:=",  oInfo.getNextTask().get(0).getTaskdefinedid());
				where.put("historystatus:in",  "1,21,2");//查询项目要么是待办处理，要么是最新待办，要么是最新，不是待办
				List<BpmHistoryEntity> oldh=historyQueryUtil.getEntitys(null, where, null, 1, 1);
				BpmHistoryEntity history1 =null;
				//d、设置项目待审批人信息
				String tsassignees="";
				boolean finder=false;
				if(oldh!=null && oldh.size()>0) {

					//3、第一个节点历史配置
					 history1 =oldh.get(0);
					 finder=true;
					//项目来源节点处理
					 if(fromTaskID.equals(history1.getfromtaskdefid())
							 || (history1.getfromtaskdefid()!=null && history1.getfromtaskdefid().contains(fromTaskID))) {
						 
					 }else {
						 if(history1.getfromtaskdefid()==null) {
								//a、设置项目来源信息
								history1.setfromtaskdefname(fromTaskName);
								history1.setfromtaskdefid(fromTaskID);
						 }else {
								//a、设置项目来源信息
								history1.setfromtaskdefname(history1.getfromtaskdefname()+","+fromTaskName);
								history1.setfromtaskdefid(history1.getfromtaskdefname()+","+fromTaskID);
						 }
					 }
					//用户处理
					 if(user.getUserId().equals(history1.getfromassigneesid())
							 || (history1.getfromassigneesid()!=null && history1.getfromassigneesid().contains(user.getUserId()))) {
						 
					 }else {
						 if(history1.getfromassigneesid()==null) {
								//a、设置项目来源信息
								history1.setfromassigneesid(user.getUserId());
								history1.setfromassigneesname(user.getUserName());
						 }else {
								//a、设置项目来源信息
								history1.setfromassigneesid(history1.getfromassigneesid()+","+user.getUserId());
								history1.setfromassigneesname(history1.getfromassigneesname()+","+user.getUserName());
						 }
					 }
					 //组织机构处理
					 if(user.getUserOrgCode().equals(history1.getfromorgid())
							 || (history1.getfromorgid()!=null && history1.getfromorgid().contains(user.getUserOrgCode()))) {
						 
					 }else {
						 if(history1.getfromorgid()==null) {
								//a、设置项目来源信息
								history1.setfromorgid(user.getUserOrgCode());
								history1.setfromorgname(user.getUserOrgName());
						 }else {
								//a、设置项目来源信息
								history1.setfromassigneesid(history1.getfromorgid()+","+user.getUserOrgCode());
								history1.setfromassigneesname(history1.getfromorgname()+","+user.getUserOrgName());
						 }
					 }
				}else {

					//3、第一个节点历史配置
					 history1 =historyEditUtl.CreateHistory(oInfo.getSexeid(), overWrokH.getactdefid(), user,itask, entity);
					//a、设置项目来源信息
					history1.setfromtaskdefname(fromTaskName);
					history1.setfromtaskdefid(fromTaskID);
					
					history1.setfromassigneesid(user.getUserId());
					history1.setfromassigneesname(user.getUserName());
					history1.setfromorgid(user.getUserOrgCode());
					history1.setfromorgname(user.getUserOrgName());
				}
				
				//b、设置项目历史状态信息
				if(index<1) {
					history1.sethistorystatus(historyStatus);
				}else {
					history1.sethistorystatus("1");
				}
				index++;
				
				//c、设置项目历史审批人问题
				String sassignees=","+user.getUserId()+","+user.getUserName();
				if(o_sassignees==null) {
					
				}else if(!o_sassignees.contains(sassignees)){
					sassignees=sassignees+","+o_sassignees;
				}else {
					sassignees=o_sassignees;
				}
				history1.setsassignees(sassignees);
				//d、设置项目待审批人信息
				//String tsassignees="";
				List<UserInfo> userlst=itask.getUser();
				for(UserInfo iuser:userlst) {
					if(iuser.getUserSelected() && !tsassignees.contains(tsassignees+","+iuser.getUserId()+",")) {
						tsassignees=tsassignees+","+iuser.getUserId()+","+iuser.getUserName()+",";
					}
				}
				
				history1.setToptassigneese(tsassignees);
				history1.setSread("0");
				
				//e、设置项目的下一个环节
				history1.setTasktype(workFlowDrive.getTaskStringType(oInfo.getbpmkey(),itask.getTaskdefinedid()));
				history1.setNexttask(this.nextTaskUtils.getNextTaskInfo(oInfo.getSexeid(), oInfo.getbpmkey(),itask.getTaskdefinedid(),entity).getNextTaskName());
				if(finder) {
					updatelst.add(history1);
				}else {
					addlst.add(history1);
				}
			}
		}
		for(TaskInfo itask:oInfo.getcTask()) {
			//workFlowDrive.SignMultiSingleTask(oInfo.getSexeid(),toWorkSelectUser,user,itask);
			workFlowDrive.SignCommonTask(oInfo.getSexeid(), user, itask, ComValue);
		}
		historyEditUtl.updateList(updatelst);
		historyEditUtl.addList(addlst);
	}
	
	/**
	 * 项目办结处理
	 * @param oInfo
	 * @param entity
	 * @param user
	 * @param o_sassignees
	 * @param overWrokH
	 * @param fromTaskName
	 * @param fromTaskID
	 * @param ComValue
	 * @param historyStatus
	 */
	public void publishedProcess(OptionInfo oInfo, IBpmProjectEntity entity, UserInfo user, String o_sassignees,
			BpmHistoryEntity overWrokH, String fromTaskName, String fromTaskID, HashMap<String, Object> ComValue,String historyStatus) {
		
		BpmHistoryEntity history=this.historyEditUtl.CreateHistory(oInfo.getSexeid(), oInfo.getbpmkey(), user, null, entity);
		history.settaskdefid("U9");
		history.settaskdefname("办结");
		history.setoptresult("办结");
		history.sethistorystatus("20");
		String sassignees=","+user.getUserId()+","+user.getUserName();
		if(o_sassignees==null) {
			
		}else if(!o_sassignees.contains(sassignees)){
			sassignees=sassignees+","+o_sassignees;
		}else {
			sassignees=o_sassignees;
		}
		history.setsassignees(sassignees);
		history.setfromtaskdefid(fromTaskID);
		history.setfromtaskdefname(fromTaskName);
		history.setoptassigneesid(user.getUserId());
		history.setoptassigneesname(user.getUserName());
		history.setoptorgid(user.getUserOrgCode());
		history.setoptorgname(user.getUserOrgName());
		history.setopttime(new Date());
		this.historyEditUtl.addandSaveEntity(history);
		for(TaskInfo itask:oInfo.getcTask()) {
			workFlowDrive.SignCommonTask(oInfo.getSexeid(), user, itask, ComValue);
		}
	}
}
